﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapperDemo.DTO;
using AutoMapperDemo.Model;
using Microsoft.AspNetCore.Mvc;

namespace AutoMapperDemo.Controllers
{
    public class UserController : Controller
    {
        private readonly IMapper _mapper;

        public UserController(IMapper mapper)
        {
            _mapper = mapper;
        }

        public IActionResult Index()
        {
            var user = new User()
            {
                Id = 0,
                Name = "测试1",
                ClassInfos = new List<ClassInfo>()
                {
                    new ClassInfo(){ Id = 1, ClassName = "班级1" },
                    new ClassInfo(){ Id = 2, ClassName = "班级2" }
                },
            };

            var dto = _mapper.Map<UserDto>(user);

            dto.ClassInfoDtos[0].ClassName = "班级1_DTO";


            return Json(new { user, dto });
        }
    }
}